import { showLoading } from '@app-nextmesh-env/nextmesh/components';
import { ITreeNode } from '@app-nextmesh-env/nextmesh/components/types';
import { documentV2 } from '@app-nextmesh-env/nextmesh/domain';
import { nmAppErrorLog } from '@app-nextmesh/common/utils';
import { cloneDeep } from 'lodash';
import { useEffect } from 'react';
import { IProps } from './typs';

interface IItemProps extends IProps {
  data: ITreeNode;
  parentIndex: number;
  formIndex: number;
}
export default function VisualizationPlotCmdDelete({
  data,
  parentIndex,
  formIndex,
  onUpdate,
  onClose,
}: IItemProps) {
  useEffect(() => {
    let hide: () => void;
    const formData = cloneDeep(data.formData);
    const items = formData.plots[parentIndex].commands;
    if (items) {
      items.splice(formIndex, 1);
      hide = showLoading();
      documentV2
        .updatePostprocess(formData)
        .then(() => {
          onUpdate(formData);
        })
        .catch((error) => {
          nmAppErrorLog('VisualizationPlotCmdDelete', error);
        })
        .finally(() => {
          hide();
          onClose();
        });
    }
  }, [data, parentIndex, formIndex]);
  return <></>;
}
